package cn.edu.neusoft.yuejian13.yuejian_13.adapter;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import com.squareup.picasso.MemoryPolicy;
import com.squareup.picasso.Picasso;

import java.util.List;

import cn.edu.neusoft.yuejian13.yuejian_13.R;
import cn.edu.neusoft.yuejian13.yuejian_13.activity.TrainDetailActivity;
import cn.edu.neusoft.yuejian13.yuejian_13.javaBean.Train;



public class TrainListAdapter extends RecyclerView.Adapter<TrainListAdapter.ViewHolder>{

    private List<Train> mTrainList;
    private Context mContext;

    public TrainListAdapter(Context mContext, List<Train> mTrainList){
        this.mContext = mContext;
        this.mTrainList = mTrainList;
        //mLayoutInflater = LayoutInflater.from(mContext);
    }



    @Override
    public TrainListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.train_cardview,parent,false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }

    @Override
    public void onBindViewHolder(TrainListAdapter.ViewHolder holder, int position) {

        final Train train = mTrainList.get(position);

        Picasso.with(mContext)
                .load("http://10.0.2.2/yuejian10/public/static/asstes/images/"+train.getPic())
                .placeholder(R.drawable.ic_default_img)
                .memoryPolicy(MemoryPolicy.NO_CACHE)
                .into(holder.trainImg);
        holder.trainName.setText(train.getTrain_name());
        holder.train_container.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, TrainDetailActivity.class);
                intent.putExtra("trainid",train.getId());
                mContext.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return mTrainList.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {

        ImageView trainImg;
        TextView trainName;
        FrameLayout train_container;

        public ViewHolder(View view){
            super(view);
            trainImg= (ImageView)view.findViewById(R.id.train_img);
            trainName = (TextView)view.findViewById(R.id.train_name);
            train_container = (FrameLayout)view.findViewById(R.id.train_container);
        }
    }


}
